package date;

import java.util.Calendar;

/**
 * Creared with IntelliJ IDEA.
 * Description:Calendar 日期类
 * User:yxd
 * Date:2022-05-13
 * Time:20:27
 */
public class CalenderDemo {
    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,15);//修改当前日期
        int day1 = calendar.get(Calendar.DAY_OF_WEEK);//获取当前星期数 美国记星期从周日(1)开始到周六(7)
        System.out.println(day1);
//        System.out.println(calendar.getTime().toLocaleString());//打印当前时间 2022-5-13 20:03:21
//        System.out.println(calendar.getTime().toString());//打印当前时间 Fri May 13 20:04:16 CST 2022
//        System.out.println(calendar.getTimeInMillis());//1970年到现在的毫秒数 1652443500495

        //获取时间信息
        int year = calendar.get(Calendar.YEAR);
        System.out.println(year);//获取当前年份 2022

        int month = calendar.get(Calendar.MONTH);
        System.out.println(month);//获取当前月份 4 需要加1

        int day = calendar.get(Calendar.DATE);
        System.out.println(day);//获取当前日 13

        int hour = calendar.get(Calendar.HOUR);
        System.out.println(hour);//获取当前小时 8

        int minute = calendar.get(Calendar.MINUTE);
        System.out.println(minute);//获取当前分钟 11

        int second = calendar.get(Calendar.SECOND);
        System.out.println(second);//获取当前秒 24

//        修改时间
//        set方法
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.DAY_OF_MONTH,5);//修改当前日为5号
        System.out.println(calendar2.getTime().toLocaleString());

        //add方法
        calendar2.add(Calendar.HOUR,1);//给这个时间的小时加1
        System.out.println(calendar2.getTime().toLocaleString());

        //补充方法
        int max = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);//当月的最大天数
        int min = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH);//最小
        System.out.println(max);
        System.out.println(min);
    }
}
